package spring.cloud.tasks.executor.name_space.executor.task.task_listener.task_statistics;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.executor.name_space.executor.ExecutorIdToTaskIdToTaskExecutorMapMap;
import spring.cloud.tasks.executor.name_space.executor.task.TaskExecutor;
import spring.cloud.tasks.executor.name_space.executor.task.task_listener.trigger_task_execute.TaskExecutorInfoService;

import java.util.List;

/**
 * 统计处理数据数量的任务.
 */
@Slf4j
@Setter
@Getter
public class ExecutorTaskStatisticsReportTask implements Runnable {

    private String executorId;

    public ExecutorTaskStatisticsReportTask(String executorId) {
        this.executorId = executorId;

    }

    @Override
    public void run() {
        List<String> taskIdList = ExecutorIdToTaskIdToTaskExecutorMapMap.getTaskIdList(executorId);
        for (String taskId : taskIdList) {
            TaskExecutor taskExecutor = ExecutorIdToTaskIdToTaskExecutorMapMap.getTaskExecutor(executorId, taskId);
            TaskExecutorInfoService taskExecutorInfoService = taskExecutor.getTaskExecutorInfoService();


            taskExecutorInfoService.persistTotalCountDelta();
            taskExecutorInfoService.persistFailureCountDelta();

            taskExecutorInfoService.persistProcessSuccessCount(TaskStatistics.getProcessSuccessCount(taskId, taskExecutorInfoService.getExecutorId()));
            taskExecutorInfoService.persistProcessFailureCount(TaskStatistics.getProcessFailureCount(taskId, taskExecutorInfoService.getExecutorId()));

        }


    }
}
